home *** CD-ROM | disk | FTP | other *** search
/ Hottest 6 / Hottest 6 (1996)(PDSoft)[!].iso / software / programming / c / sipp / demo / animation / animation.c next >
Encoding:
C/C++ Source or Header  |  1978-11-24  |  6.3 KB  |  265 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #include <geometric.h>
  5. #include <sipp.h>
  6. #include <shaders.h>
  7. #include <primitives.h>
  8.  
  9. extern double atof();
  10.  
  11. extern char *optarg;
  12. extern int optind,  opterr;
  13.  
  14.  
  15. #define RESOLUTION    5
  16. #define FLOORSIZE     15.0
  17.  
  18.  
  19. Marble_desc teapot_surf = {
  20.     0.4, 
  21.     0.5, 
  22.     0.05, 
  23.     8.0, 
  24.     {0.90, 0.80, 0.65}, 
  25.     {0.30, 0.08, 0.08}, 
  26.     {1.0, 1.0, 1.0}
  27. };
  28.  
  29.  
  30. typedef struct {
  31.     double   sqsize;
  32.     Surf_desc   col1;
  33.     Surf_desc   col2;
  34. } Floor_desc;
  35.  
  36.  
  37. Floor_desc floor_surf = {
  38.     1.0,
  39.     { 0.4, 0.0, 0.1, {0.9900, 0.9000, 0.7900}, {1.0, 1.0, 1.0} },
  40.     { 0.4, 0.0, 0.1, {0.8300, 0.2400, 0.1000}, {1.0, 1.0, 1.0} }
  41. };
  42.  
  43.  
  44. /*
  45.  * A shader to produce a checkered floor.
  46.  */
  47. static void
  48. floor_shader(pos, normal, texture, view_vec, lights, fd, color, transp)
  49.     Vector      *pos;
  50.     Vector      *normal;
  51.     Vector      *texture;
  52.     Vector      *view_vec;
  53.     Lightsource *lights;
  54.     Floor_desc  *fd;
  55.     Color       *color;
  56.     Color       *transp;
  57. {
  58.     Surf_desc  * col;
  59.     int          intu;
  60.     int          intv;
  61.  
  62.     intu = floor(texture->x / fd->sqsize);
  63.     if (intu < 0)
  64.         intu = -intu;
  65.  
  66.     intv = floor(texture->y / fd->sqsize);
  67.     if (intv < 0)
  68.         intv = -intv;
  69.  
  70.     if ((intu ^ intv) & 1)
  71.         col = &fd->col1;
  72.     else
  73.         col = &fd->col2;
  74.  
  75.     basic_shader(pos, normal, texture, view_vec, lights, col, color, transp);
  76. }
  77.  
  78.  
  79.  
  80. main(argc, argv)
  81.     int     argc;
  82.     char  **argv;
  83. {
  84.     Object  *teapot;        /* The teapot and its parts. */
  85.     Object  *handle;
  86.     Object  *spout;
  87.     Object  *body;
  88.     Object  *lid;
  89.     Object  *bottom;
  90.     Transf_mat  * teapot_transf;
  91.  
  92.     Object  *floor;        /* The floor. */
  93.  
  94.     FILE    *infile;
  95.     int      image_size;
  96.     FILE    *image;
  97.     int      frame;
  98.     int      mode;
  99.  
  100.     double   time_start;    /* Animation time values. */
  101.     double   time_stop;
  102.     double   time_step;
  103.     double   time;
  104.     double   t_jump;
  105.     double   height;
  106.     double   t_scale;
  107.     double   xyscaling;
  108.     double   zscaling;
  109.     double   angle;
  110.     char     filename[256];
  111.     char     *file_ext;
  112.     int      c;
  113.  
  114.     mode = LINE;
  115.     time_start = 0.0;
  116.     time_stop  = 1.0;
  117.     time_step  = 0.04;
  118.     image_size = 256;
  119.     file_ext = "pbm";
  120.  
  121.     while ((c = getopt(argc, argv, "pgfls:")) != EOF) {
  122.         switch (c) {
  123.           case 'p':
  124.             mode = PHONG;
  125.             file_ext = "ppm";
  126.             break;
  127.  
  128.           case 'g':
  129.             mode = GOURAUD;
  130.             file_ext = "ppm";
  131.             break;
  132.  
  133.           case 'f':
  134.             mode = FLAT;
  135.             file_ext = "ppm";
  136.             break;
  137.  
  138.           case 'l':
  139.             mode = LINE;
  140.             file_ext = "pbm";
  141.             break;
  142.  
  143.           case 's':
  144.             image_size = atoi(optarg);
  145.             break;
  146.         }
  147.     }
  148.  
  149.  
  150.     sipp_init();
  151.  
  152.  
  153.     /* Create the floor. */
  154.     floor = sipp_block(FLOORSIZE, FLOORSIZE, 1.0, &floor_surf, floor_shader,
  155.                        WORLD);
  156.     object_move(floor, 0.0, 0.0, -0.5);
  157.     object_add_subobj(sipp_world, floor);
  158.  
  159.  
  160.     /* Create the teapot and its parts. */
  161.     infile = fopen("../tpt_handle.bez", "r");
  162.     handle = sipp_bezier_file(infile, RESOLUTION, &teapot_surf, marble_shader,
  163.                               WORLD);
  164.     fclose(infile);
  165.  
  166.     infile = fopen("../tpt_spout.bez", "r");
  167.     spout = sipp_bezier_file(infile, RESOLUTION, &teapot_surf, marble_shader,
  168.                              WORLD); 
  169.     fclose(infile);
  170.  
  171.     infile = fopen("../tpt_body.bez", "r");
  172.     body = sipp_bezier_file(infile, RESOLUTION, &teapot_surf, marble_shader,
  173.                             WORLD); 
  174.     fclose(infile);
  175.  
  176.     infile = fopen("../tpt_lid.bez", "r");
  177.     lid = sipp_bezier_file(infile, RESOLUTION, &teapot_surf, marble_shader,
  178.                            WORLD); 
  179.     fclose(infile);
  180.  
  181.     bottom = sipp_cylinder(0.375, 0.01, RESOLUTION * 4, &teapot_surf,
  182.                            marble_shader, WORLD);
  183.  
  184.     teapot = object_create();
  185.     object_add_subobj(teapot, body);
  186.     object_add_subobj(teapot, lid);
  187.     object_add_subobj(teapot, handle);
  188.     object_add_subobj(teapot, spout);
  189.     object_add_subobj(teapot, bottom);
  190.     object_add_subobj(sipp_world, teapot);
  191.  
  192.  
  193.     /* Lit the stage! */
  194.     lightsource_create(0.2, -2.0, 1.0, 1.0, 1.0, 1.0, LIGHT_DIRECTION);
  195.     lightsource_create(1.0, 0.0, 0.5, 0.4, 0.4, 0.4, LIGHT_DIRECTION);
  196.  
  197.  
  198.     /* Viewing parameters. */
  199.     camera_position(sipp_camera, 16.0, -24.0, 4.0);
  200.     camera_look_at(sipp_camera, 0.0, 0.0, 1.4);
  201.     camera_up(sipp_camera, 0.0, 0.0, 1.0);
  202.     camera_focal(sipp_camera, 0.0625);
  203.  
  204.  
  205.     /*
  206.      * The following code is quite ugly and full of magic numbers.
  207.      * It is basically two parabolas that describe the teapots jump 
  208.      * and its "squashing" when it lands.
  209.      */
  210.     frame = time_start / time_step;
  211.     time = frame * time_step;
  212.     while (time < time_stop) {
  213.  
  214.         if (time < 0.65) {
  215.         /* During the jump */
  216.             t_jump = time * 1.94464;
  217.             height = 6.2 * t_jump - 9.81 * t_jump * t_jump / 2.0;
  218.             angle = 2.0 * M_PI * time / 0.65;
  219.             xyscaling = 1.0;
  220.             zscaling = 1.0;
  221.  
  222.         } else {
  223.         /* During the squashing phase. */
  224.             height = 0.0;
  225.             angle = 0.0;
  226.             t_scale = (time - 0.65) * 0.64533;
  227.             xyscaling = 1.0 + (1.0 - zscaling) * M_SQRT1_2;
  228.             zscaling = 1.0 - 6.2 * t_scale + 54.9 * t_scale * t_scale / 2.0;
  229.         }
  230.  
  231.     /* 
  232.      * Save the original transformation state of the teapot.
  233.      * It is easier to recreate the proper position from scratch
  234.      * for each new frame than to calculate the difference between
  235.      * one image and the next.
  236.      */
  237.     teapot_transf = object_get_transf(teapot, NULL);
  238.  
  239.     /* Place the teapot in its proper position. */
  240.         object_scale(teapot, xyscaling, xyscaling, zscaling);
  241.         object_move(teapot, 0.0, 0.0, -0.4);
  242.         object_rot_y(teapot, -angle);
  243.         object_move(teapot, 0.0, 0.0, 0.4);
  244.         object_move(teapot, 0.0, 0.0, height);
  245.  
  246.         sprintf(filename, "anim%02d.%s", frame, file_ext);
  247.         image = fopen(filename, "w");
  248.         printf("\rRendering frame %2d...", frame);
  249.         fflush(stdout);
  250.  
  251.     /* Render the image. */
  252.         render_image_file(image_size, image_size, image, mode, 3);
  253.         fclose(image);
  254.  
  255.     /* Reset the teapot to its original position and shape. */
  256.     object_set_transf(teapot, teapot_transf);
  257.  
  258.         frame++;
  259.         time = frame * time_step;
  260.     }
  261.  
  262.     printf("done.\n");
  263.     exit(0);
  264. }
  265.